package com.videostreaming.db;

import com.videostreaming.data.Video;
import com.videostreaming.data.User;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 *
 * @author sad
 */
public interface DB {

    @Select("SELECT * FROM user")
    public List<User> getUsers();

    @Select("SELECT * FROM user where id=#{id}")
    public List<User> getUser(int id);

    @Select("SELECT * FROM user where email=#{email}")
    public User getUserByEmail(@Param("email") String email);

    @Select("SELECT * FROM user where email=#{email} and password=#{password}")
    public User getUserByEmailAndPassword(@Param("email") String email, @Param("password") String password);

    @Select("SELECT * FROM user where userSecretHash=#{secretHash}")
    public User getUserBySecretHash(@Param("secretHash") String secretHash);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into USER(firstName, lastName, password, email, userSecretHash)VALUES(#{firstName},#{lastName}, #{password}, #{email}, #{userSecretHash})")
    public void insertUser(User user);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into playvideo(videoHash, userId)VALUES(#{videoHash}, #{userId})")
    public void insertVideo(Video playVideo);

    @Delete("delete from playvideo where videoHash=#{videoHash}")
    public void deleteVideoByHash(@Param("videoHash") String videoHash);

    @Delete("delete from playvideo where userId=#{userId}")
    public void deleteVideoByUserId(@Param("userId") int userId);

    @Select("SELECT * FROM playvideo where play=1")
    public List<Video> getActiveVideos();

    @Select("SELECT * FROM playvideo where userId=#{userId}")
    public Video getVideoForUser(@Param("userId") int userId);

    @Update("UPDATE playvideo SET play=#{playStatus}where userId=#{userId}")
    public void setVideoStatusForUser(@Param("userId") int userid, @Param("playStatus") boolean playStatus);
}
